#include "key.h"
unsigned char keynum = 255;
sbit row3 = P3^2;
sbit row4 = P3^3;
sbit col1 = P4^4;
sbit col2 = P4^2;

static void clean_row(void)
{
	row3 = 1;
	row4 = 1;
	col1 = 0;
	col2 = 0;
}

static void clean_col(void)
{
	row3 = 0;
	row4 = 0;
	col1 = 1;
	col2 = 1;
}

void scan_key(void)
{
	static unsigned char status = 0;
	static unsigned char midvalue = 0;
	static unsigned char row = 0;
	static unsigned char num = 0;
	switch(status)
	{
		case 0:
			clean_row();
			if(row3==0||row4==0)
			{
				status = 1;
			}
			break;
		case 1:
			clean_row();
			if(row3==0)
			{
				row = 1;
				status = 2;
			}
			else if(row4 == 0)
			{
				row = 0;
				status = 2;
			}
			else
			{
				status = 0;
			}
			break;
		case 2:
			clean_col();
			if(col1 == 0)
			{
				midvalue = 4+row;
				status = 3;
			}
			else if(col2 == 0)
			{
				midvalue = 8+row;
				status = 3;
			}
			else
			{
				status = 0;
			}
			break;
		case 3:
			clean_col();
			if(col1==1&&col2==1)
			{
				if(num == 50)
					keynum = 10;
				else
					keynum = midvalue;
				num = 0;
				status = 0;
			}
			else if(col2 == 0)
			{
				clean_row();
				if(row3==0 && row4==0&&num < 50)
				{
					num++;
				}
			}
			break;
	}

}